// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import utils from './components/public/utils'
import router from './router'
import VueClipboard from 'vue-clipboard2'
// import Vuex from 'vuex'
// import store from './store/store'
import axios from 'axios'
import md5 from 'js-md5';
// import Share from 'vue-social-share'
// import 'vue-social-share/dist/client.css';
import { Overlay ,Field ,Popup,Switch ,Toast,Progress,CountDown,Checkbox,Swipe, SwipeItem } from 'vant';

Vue.prototype.$md5 = md5;
Vue.prototype.$http = axios;
Vue.prototype.utils = utils;
Vue.prototype.$addStorageEvent = function (key, data) {
  // 创建一个StorageEvent事件
  var newStorageEvent = document.createEvent('StorageEvent');
  const storage = {
      setItem: function (k, val) {
          localStorage.setItem(k, val);
          // 初始化创建的事件
          newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
          // 派发对象
          window.dispatchEvent(newStorageEvent);
      }
  }
  return storage.setItem(key, data);
}

Vue.directive('focus', {
  // 当被绑定的元素插入到 DOM 中时……
  inserted: function (el) {
    // 聚焦元素
    el.focus()
  }
})
router.afterEach((to, from, next) => {
  window.scrollTo(0, 0)
});
let loadinginstace
axios.interceptors.request.use(
  config => {
    if (localStorage.getItem('z_token')) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      config.headers['Authorization'] = `${localStorage.getItem('z_token')}`;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });
  Toast.setDefaultOptions({ duration: 2000 });
// http响应拦截器
axios.interceptors.response.use(data => {
  const code = data.data.code;
  if(code == -1) { //未登录
    localStorage.clear();
      Toast('登录失效，请重新登录！');
      router.push('/login');
　　}

　　return data
}, error => {
　　const code = error.data.code;
　　// 此处判断拦截需要处理的错误状态码并处理
　　if(code == 502) {
　　
　　}

　　return Promise.reject(error) 
})

// Vue.use(Share)
// Vue.use(Vuex)
Vue.use(Overlay).use(Field).use(Popup).use(Switch).use(Toast).use(Progress).use(CountDown).use(Checkbox).use(Swipe).use(SwipeItem);
Vue.use(VueClipboard)
Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
